using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MvcExtExample.Views.Shared
{
    public partial class Handler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Request.Form["mName"].ToString();
            string pwd = Request.Form["mPwd"].ToString();
            SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            ts.Open();
            string pw = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5");
            string sql = "select * from Tb_User where User_ID = '" + name + "'and Password = '" + pw + "'";
            SqlCommand cop = new SqlCommand(sql, ts);
            SqlDataAdapter sda = new SqlDataAdapter(cop);
            DataSet ds = new DataSet();
            sda.Fill(ds, "Tb_User");
            if (ds.Tables[0].DefaultView.Count < 1)
            {
                Response.Write("用户名或密码错误");
                Response.End();
            }
            DataRowView rowview = ds.Tables[0].DefaultView[0];    //定义一个视图
            string Privilege = rowview["Privilege"].ToString();
            //int c = Convert.ToInt32(cop.ExecuteScalar());//获得此用户名和密码在数据库中有多少个匹配的
            if (Privilege == null)
            {
                Response.Write("用户名或密码错误");
                Response.End();
            }
            else
            {
                if (Privilege != null && Privilege != "")
                {
                    int privilege = Convert.ToInt32(Privilege);
                    if (privilege == 8)//如果是管理员
                    {
                        Response.Write("admin");
                        Response.End();
                    }
                }
                Session["LogedUser"] = name;
                Response.Write("right");
                Response.End();
            }
            ts.Close();
        }
    }
}